import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class Solution2 {
    static class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }

        public Node copyRandomList(Node head) {
            Node cur = head;
            HashMap<Node, Node> map = new HashMap<>();
            while (cur != null) {
                Node newNode = new Node(cur.val);
                map.put(cur,newNode);
                cur = cur.next;
            }
            cur=head;
            while (cur != null) {
                map.get(cur).next = map.get(cur.next);
                map.get(cur).random = map.get(cur.random);
                cur=cur.next;
            }
            return map.get(head);
        }

    }
}
